<?php
class test{
	function xfyun(){
		    $daytime=strtotime('1970-1-1T00:00:00 UTC');
		    $api = "http://api.xfyun.cn/v1/service/v1/iat ";
		    $XAppid = "5c6ca376";
		    $Apikey = "f225a0173076b04111d09ebed70e0d60";
		    $XCurTime =time();
		    $XParam ="";
		    $XCheckSum ="";
		    
		    $Param= array(
		        "aue"=>"raw",
		        "engine_type"=>"sms16k",
		    );

		    $filePath = "upload/1550737192.wav";
		    $fp = fopen($filePath, "rb");
		    $audioContent = fread($fp,filesize($filePath));
		    $audioBase64 = base64_encode($audioContent);
		    
		    $Post = array(
			  'audio' => $audioBase64,
			);

		    $XParam = base64_encode(json_encode($Param));
		    $XCheckSum = md5($Apikey.$XCurTime.$XParam);
		    $headers = array();
		    $headers[] = 'X-CurTime:'.$XCurTime;
		    $headers[] = 'X-Param:'.$XParam;
		    $headers[] = 'X-Appid:'.$XAppid;
		    $headers[] = 'X-CheckSum:'.$XCheckSum;
		    $headers[] = 'Content-Type:application/x-www-form-urlencoded; charset=utf-8';
		    echo $this->http_request($api, $Post, $headers);
		}

		/**
		 * 发送post请求
		 * @param string $url 请求地址
		 * @param array $post_data post键值对数据
		 * @return string
		 */
		function http_request($url, $post_data, $headers) {		 
		  $postdata = http_build_query($post_data);
		  $options = array(
		    'http' => array(
		      'method' => 'POST',
		      'header' => $headers,
		      'content' => $postdata,
		      'timeout' => 15 * 60 // 超时时间（单位:s）
		    )
		  );
		  $context = stream_context_create($options);
		  $result = file_get_contents($url, false, $context);
			
		  echo $result; 
			
		  return "success";
		}
}
$a = new test();
$a->xfyun();
?>